package org.video.stream;

import android.util.Log;
import android.view.Surface;

/**
 * Created by 晨 on 2018/4/18.
 */

public class Mmixer {
    private static final String TAG = "corelib";
    static {
        try {
            System.loadLibrary("mmsmpr_mmix");
        }
        catch (UnsatisfiedLinkError ule) {
            Log.e(TAG, "Can't load mmsmpr_mmix library: " + ule);
            /// FIXME Alert user
            System.exit(1);
        } catch (SecurityException se) {
            Log.e(TAG, "Encountered a security issue when loading mmsmpr_play library: " + se);
            /// FIXME Alert user
            System.exit(1);
        }
    }
    private long mInst = 0;
    private Aout mAout = null;

    public Mmixer()
    {

    }

    public int init() {
        if (mInst == 0) {
            mInst = init0();
        }

        if (mInst != 0) {
            return 0;
        } else {
            return -1;
        }
    }
    public int destroy()
    {
        if (mInst != 0)
        {
            return destroy(mInst);
        }
        else
        {
            return -1;
        }
    }
    public long newCh(String url, boolean loop,
                      Surface surf,
                      boolean video, float prevX, float prevY, float prevW, float prevH,
                      float liveX, float liveY, float liveW, float liveH,
                      boolean audio, float volume)
    {
        if(mInst != 0)
        {
            return newCh(mInst, url, loop, surf, video, prevX, prevY, prevW, prevH,
                          liveX, liveY, liveW, liveH,
                          audio, volume);
        }
        else {
            return 0;
        }
    }
    public long newCameraCh(String url, boolean loop,
                      Surface surf,
                      boolean video, int width, int height, float prevX, float prevY, float prevW, float prevH,
                      float liveX, float liveY, float liveW, float liveH,
                      boolean audio, float volume)
    {
        if(mInst != 0)
        {
            return newCameraCh(mInst, url, loop, surf, video, width, height, prevX, prevY, prevW, prevH,
                    liveX, liveY, liveW, liveH,
                    audio, volume);
        }
        else {
            return 0;
        }
    }
    public long newMicrophoneCh(String url, boolean loop,
                      Surface surf,
                      boolean video, float prevX, float prevY, float prevW, float prevH,
                      float liveX, float liveY, float liveW, float liveH,
                      boolean audio, float volume)
    {
        if(mInst != 0)
        {
            return newMicrophoneCh(mInst, url, loop, surf, video, prevX, prevY, prevW, prevH,
                    liveX, liveY, liveW, liveH,
                    audio, volume);
        }
        else {
            return 0;
        }
    }
    public int inputCh(long ch, byte[] yuv)
    {
        if (mInst != 0)
        {
            return inputCh(mInst, ch, yuv);
        }
        else
        {
            return 0;
        }
    }
    public long chgCh(long ch, boolean loop,
                      Surface surf,
                      boolean video, float prevX, float prevY, float prevW, float prevH,
                      float liveX, float liveY, float liveW, float liveH,
                      boolean audio, float volume)
    {
        if(mInst != 0)
        {
            return chgCh(mInst, ch, loop, surf, video, prevX, prevY, prevW, prevH,
                    liveX, liveY, liveW, liveH,
                    audio, volume);
        }
        else {
            return 0;
        }
    }
    public int delCh(long ch)
    {
        if (mInst != 0)
        {
            return delCh(mInst, ch);
        }
        else
        {
            return -1;
        }
    }
    //预览窗口
    public int attachPSurface(Surface surf)
    {
        if (mInst != 0)
        {
            return attachPSurface(mInst, surf);
        }
        else
        {
            return -1;
        }
    }
    public int detachPSurface()
    {
        if (mInst != 0)
        {
            return detachPSurface(mInst);
        }
        else
        {
            return -1;
        }
    }
    //直播窗口
    public int attachLSurface(Surface surf)
    {
        if (mInst != 0)
        {
            return attachLSurface(mInst, surf);
        }
        else
        {
            return -1;
        }
    }
    public int detachLSurface()
    {
        if (mInst != 0)
        {
            return detachLSurface(mInst);
        }
        else
        {
            return -1;
        }
    }
    //大屏输出
    public int attachSSurface(Surface surf)
    {
        if (mInst != 0)
        {
            return attachSSurface(mInst, surf);
        }
        else
        {
            return -1;
        }
    }
    public int detachSSurface()
    {
        if (mInst != 0)
        {
            return detachSSurface(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int startVPrev()
    {
        if (mInst != 0)
        {
            return startVPrev(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int stopVPrev()
    {
        if (mInst != 0)
        {
            return stopVPrev(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int startVLive()
    {
        if (mInst != 0)
        {
            return startVLive(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int stopVLive()
    {
        if (mInst != 0)
        {
            return stopVLive(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int startVSec()
    {
        if (mInst != 0)
        {
            return startVSec(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int stopVSec()
    {
        if (mInst != 0)
        {
            return stopVSec(mInst);
        }
        else
        {
            return -1;
        }
    }

    public int startNMix()
    {
        if (mInst != 0)
        {
            return startNMix(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int stopNMix()
    {
        if (mInst != 0)
        {
            return stopNMix(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int startRMix()
    {
        if (mInst != 0)
        {
            return startRMix(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int stopRMix()
    {
        if (mInst != 0)
        {
            return stopRMix(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int setLVolume(float volume)
    {
        if (mInst != 0)
        {
            return setLVolume(mInst, volume);
        }
        else
        {
            return -1;
        }
    }
    public int startRtmp(String url)
    {
        if (mInst != 0)
        {
            return startRtmp(mInst, url);
        }
        else
        {
            return -1;
        }
    }
    public int stopRtmp()
    {
        if (mInst != 0)
        {
            return stopRtmp(mInst);
        }
        else
        {
            return -1;
        }
    }
    public int startRec(String url)
    {
        if (mInst != 0)
        {
            return startRec(mInst, url);
        }
        else
        {
            return -1;
        }
    }
    public int stopRec()
    {
        if (mInst != 0)
        {
            return stopRec(mInst);
        }
        else
        {
            return -1;
        }
    }

    //音频输出
    /**
     * Open the Java audio output.
     * This function is called by the native code
     */
    public void initAout(int sampleRateInHz, int channels, int samples)
    {
        //Log.d(TAG, "Opening the java audio output");
        mAout.init(sampleRateInHz, channels, samples);
    }
    /**
     * Play an audio buffer taken from the native code
     * This function is called by the native code
     */
    public void playAudio(byte[] audioData, int bufferSize)
    {
        mAout.playBuffer(audioData, bufferSize);
    }
    /**
     * Close the Java audio output
     * This function is called by the native code
     */
    public void closeAout()
    {
        Log.d(TAG, "Closing the java audio output");
        mAout.release();
    }

    private native long init0();
    private native int destroy(long inst);
    private native long newCh(long inst, String url, boolean b_loop, Surface surf,
                              boolean video, float prevX, float prevY, float prevW, float prevH,
                              float liveX, float liveY, float liveW, float liveH,
                              boolean audio, float volume);
    private native long newCameraCh(long inst, String url, boolean b_loop, Surface surf,
                              boolean video, int width, int height, float prevX, float prevY, float prevW, float prevH,
                              float liveX, float liveY, float liveW, float liveH,
                              boolean audio, float volume);
    private native long newMicrophoneCh(long inst, String url, boolean b_loop, Surface surf,
                              boolean video, float prevX, float prevY, float prevW, float prevH,
                              float liveX, float liveY, float liveW, float liveH,
                              boolean audio, float volume);
    private native int inputCh(long inst, long ch, byte[] yuv);
    private native int chgCh(long inst, long ch, boolean b_loop, Surface surf,
                              boolean video, float prevX, float prevY, float prevW, float prevH,
                              float liveX, float liveY, float liveW, float liveH,
                              boolean audio, float volume);
    private native int delCh(long inst, long ch);
    private native int attachPSurface(long inst, Surface surf);
    private native int detachPSurface(long inst);
    private native int attachLSurface(long inst, Surface surf);
    private native int detachLSurface(long inst);
    private native int attachSSurface(long inst, Surface surf);
    private native int detachSSurface(long inst);
    private native int startVPrev(long inst);
    private native int stopVPrev(long inst);
    private native int startVLive(long inst);
    private native int stopVLive(long inst);
    private native int startVSec(long inst);
    private native int stopVSec(long inst);

    private native int startNMix(long inst);
    private native int stopNMix(long inst);
    private native int startRMix(long inst);
    private native int stopRMix(long inst);
    private native int setLVolume(long inst, float volume);
    private native int startRtmp(long inst, String url);
    private native int stopRtmp(long inst);
    private native int startRec(long inst, String url);
    private native int stopRec(long inst);
}
